package com.gaohe.service;

import com.gaohe.mapper.GoodsMapper;
import com.gaohe.pojo.Goods;
import com.gaohe.untils.MybatisUntils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class GoodsService {
    public List<Goods> findAll() {
        SqlSession sqlSession = MybatisUntils.getSqlSession(false);
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        List<Goods> list = goodsMapper.findAll();
        return list;
    }

    public int add(Goods goods) {
        SqlSession sqlSession = MybatisUntils.getSqlSession(true);
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        int add = goodsMapper.add(goods);
        return add;
    }
    public int updateById(Goods goods) {
        SqlSession sqlSession = MybatisUntils.getSqlSession(true);
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        int update = goodsMapper.updateById(goods);
        return update;
    }
    public int delById(int id) {
        SqlSession sqlSession = MybatisUntils.getSqlSession(true);
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        int del = goodsMapper.delById(id);
        return del;
    }
    public List<Goods> findByName(String name) {
        SqlSession sqlSession = MybatisUntils.getSqlSession(false);
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        List<Goods> list = goodsMapper.findByName(name);
        return list;
    }
    public PageInfo<Goods> page(int pageNum, int pageSize){
        SqlSession sqlSession = MybatisUntils.getSqlSession(false);
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        PageHelper.startPage(pageNum,pageSize);
        List<Goods> list = goodsMapper.findAll();
        PageInfo<Goods> goodsPageInfo = new PageInfo<>(list);
        return goodsPageInfo;
    }
    public List<Goods> findCon(Goods goods){
        SqlSession sqlSession = MybatisUntils.getSqlSession(false);
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        List<Goods> list = goodsMapper.findCon(goods);
        return list;
    }
    public Goods getById(int id){
        SqlSession sqlSession = MybatisUntils.getSqlSession(false);
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        Goods goods = goodsMapper.getById(id);
        return goods;
    }
}
